<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">






  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-bounce.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />










  <meta name="baidu-site-verification" content="HvauOyX6Si" />













<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.4.1" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.4.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/ei_32_32.png?v=6.4.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/ei_16_16.png?v=6.4.1">


  <link rel="mask-icon" href="/images/logo.svg?v=6.4.1" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '6.4.1',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="简介上篇我们做了一些简单的运行文件的配置，本篇带领大家来认识常用的一些配置，当然了关于Spring Boot 这些配置太多太多了，如果想了解更多的话直接上官网参考一下，了解相关案例如本篇的配置。 application.properties配置官方指南参考。 Spring Boot有以下方式配置application.properties配置在 IntelliJ IDEA 开发工具中创建项目的时候">
<meta name="keywords" content="Java,Spring Boot">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Boot 配置文件设置(三)">
<meta property="og:url" content="https://eirunye.gitee.io/2018/08/20/Spring-Boot-配置文件设置-三/index.html">
<meta property="og:site_name" content="Eirunye Activity">
<meta property="og:description" content="简介上篇我们做了一些简单的运行文件的配置，本篇带领大家来认识常用的一些配置，当然了关于Spring Boot 这些配置太多太多了，如果想了解更多的话直接上官网参考一下，了解相关案例如本篇的配置。 application.properties配置官方指南参考。 Spring Boot有以下方式配置application.properties配置在 IntelliJ IDEA 开发工具中创建项目的时候">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-2eecd990df12386f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-d5479d7990d69320.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-a52caf3352427bd0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-e83c94a826f2309d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-7730447bb95f7fee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/500">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-9fb441bce0fc8ecf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-eae9ed7abb04ee43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3012005-5551aabdebf638ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:updated_time" content="2018-08-21T08:36:40.215Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring Boot 配置文件设置(三)">
<meta name="twitter:description" content="简介上篇我们做了一些简单的运行文件的配置，本篇带领大家来认识常用的一些配置，当然了关于Spring Boot 这些配置太多太多了，如果想了解更多的话直接上官网参考一下，了解相关案例如本篇的配置。 application.properties配置官方指南参考。 Spring Boot有以下方式配置application.properties配置在 IntelliJ IDEA 开发工具中创建项目的时候">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3012005-2eecd990df12386f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300">






  <link rel="canonical" href="https://eirunye.gitee.io/2018/08/20/Spring-Boot-配置文件设置-三/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Spring Boot 配置文件设置(三) | Eirunye Activity</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

<a href="https://github.com/eirunye" class="github-corner" aria-label="View source on Github"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#64CEAA; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Eirunye Activity</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">NOTES</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签<span class="badge">11</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类<span class="badge">9</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档<span class="badge">13</span></a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-schedule">
    <a href="/schedule/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />日程表</a>
  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://eirunye.gitee.io/2018/08/20/Spring-Boot-配置文件设置-三/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Eirunye">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/eirunye_icon.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Eirunye Activity">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Spring Boot 配置文件设置(三)
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-08-20 21:51:39" itemprop="dateCreated datePublished" datetime="2018-08-20T21:51:39+08:00">2018-08-20</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-08-21 16:36:40" itemprop="dateModified" datetime="2018-08-21T16:36:40+08:00">2018-08-21</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/后台/" itemprop="url" rel="index"><span itemprop="name">后台</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/后台/Spring-Boot/" itemprop="url" rel="index"><span itemprop="name">Spring Boot</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>上篇我们做了一些简单的运行文件的配置，本篇带领大家来认识常用的一些配置，当然了关于Spring Boot 这些配置太多太多了，如果想了解更多的话直接上官网参考一下，了解相关案例如本篇的配置。 <a href="https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#common-application-properties" target="_blank" rel="noopener">application.properties配置官方指南参考</a>。<br><img src="https://upload-images.jianshu.io/upload_images/3012005-2eecd990df12386f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300" alt="Spring Boot.jpg"></p>
<h1 id="Spring-Boot有以下方式配置"><a href="#Spring-Boot有以下方式配置" class="headerlink" title="Spring Boot有以下方式配置"></a>Spring Boot有以下方式配置</h1><h2 id="application-properties配置"><a href="#application-properties配置" class="headerlink" title="application.properties配置"></a>application.properties配置</h2><p>在 <a href="https://www.jetbrains.com/idea/" target="_blank" rel="noopener">IntelliJ IDEA</a> 开发工具中创建项目的时候，默认的配置文件是<em>application.properties</em>，接下来我们就学习一下然后配置一些我们在开发中经常用到的配置项，进入带领我们揭开Spring Boot 项目的神秘的面纱。<br>在下面的文档中我会在每个配置中进行注解，这样能更好的了解，当然官方文档有我们所需的全部配置，大家如果项目开发中有需求那么可以进入<a href="https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#common-application-properties" target="_blank" rel="noopener">application.properties配置官方指南参考</a>。</p>
<p><strong>英:</strong> Appendix A. Common application properties<br> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Various properties can be specified inside your application.properties file, inside your application.yml file, or as command line switches.</span><br><span class="line">This appendix provides a list of common Spring Boot properties and references to the underlying classes that consume them.</span><br><span class="line">[Note]</span><br><span class="line">Property contributions can come from additional jar files on your classpath, so you should not consider this an exhaustive list.</span><br><span class="line">Also, you can define your own properties.</span><br><span class="line">[Warning]</span><br><span class="line">This sample file is meant as a guide only. Do not copy and paste the entire content into your application. Rather, pick only the properties that you need.</span><br></pre></td></tr></table></figure></p>
<p><strong>译:</strong> 官方指南者三段话概况了今天我们要讲得内容：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">可以在application.properties文件中，application.yml文件中或命令行开关中指定各种属性。 本附录提供了常用Spring Boot属性的列表以及对使用它们的基础类的引用。</span><br><span class="line">[注意]</span><br><span class="line">属性贡献可以来自类路径上的其他jar文件，因此您不应将此视为详尽的列表。 此外，您可以定义自己的属性。</span><br><span class="line">[警告]</span><br><span class="line">此示例文件仅供参考。 不要将整个内容复制并粘贴到您的应用程序中。 相反，只选择您需要的属性。</span><br></pre></td></tr></table></figure></p>
<p>所以我们在添加某些配置属性的时候，一定要根据自己的需要来添加，不然有时出错了，不知道哪里找问题。</p>
<h3 id="常用的application-properties配置"><a href="#常用的application-properties配置" class="headerlink" title="常用的application.properties配置"></a>常用的application.properties配置</h3><p>一般情况下在src目录下的/main/resource文件夹中新建<em><code>application.properties</code></em>文件，目录结构如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">|--src</span><br><span class="line">   |--main</span><br><span class="line">      |--resources</span><br><span class="line">         |--application.properties</span><br></pre></td></tr></table></figure></p>
<p>我们接下来编写一些常用的属性配置，大家在开发中需要到哪些就直接去查看一下:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br></pre></td><td class="code"><pre><span class="line">#启用调试日志。</span><br><span class="line">debug=false</span><br><span class="line">#启用跟踪日志。</span><br><span class="line">trace=false</span><br><span class="line"></span><br><span class="line">#--------------------------------------</span><br><span class="line"># LOGGING 日记</span><br><span class="line">#--------------------------------------</span><br><span class="line"># 日志配置文件的位置。 例如，Logback的classpath:logback.xml</span><br><span class="line">logging.config=classpath:logback.xml</span><br><span class="line"># 日志文件名（例如，`myapp.log`）。名称可以是精确位置或相对于当前目录。</span><br><span class="line">logging.file=property.log</span><br><span class="line"># 最大日志文件大小。 仅支持默认的logback设置</span><br><span class="line">logging.file.max-size=10MB</span><br><span class="line"># 日志文件的位置。 例如，`/ var / log`。</span><br><span class="line">logging.path=/var/log</span><br><span class="line"></span><br><span class="line">#---------------------------------</span><br><span class="line"># AOP</span><br><span class="line">#---------------------------------</span><br><span class="line"># 使用AOP 切面编程</span><br><span class="line">spring.aop.auto=true</span><br><span class="line">#是否要创建基于子类的（CGLIB）代理（true），而不是基于标准Java接口的代理（false）</span><br><span class="line">spring.aop.proxy-target-class=true</span><br><span class="line"></span><br><span class="line">#--------------------------------</span><br><span class="line"># Email</span><br><span class="line">#--------------------------------</span><br><span class="line"># 编码格式</span><br><span class="line">spring.mail.default-encoding=UTF-8</span><br><span class="line"># SMTP服务器主机</span><br><span class="line">spring.mail.host=smtp.property.com</span><br><span class="line">#SMTP服务器端口</span><br><span class="line">spring.mail.port=7800</span><br><span class="line"># 登录SMTP用户名</span><br><span class="line">spring.mail.username=property</span><br><span class="line"># 登录SMTP密码</span><br><span class="line">spring.mail.password=123456</span><br><span class="line"></span><br><span class="line">#--------------------------------</span><br><span class="line"># WEB 属性配置</span><br><span class="line">#--------------------------------</span><br><span class="line"># 服务器应绑定的网络地址</span><br><span class="line">server.address=127.0.0.1</span><br><span class="line"># 是否启用了响应压缩</span><br><span class="line">server.compression.enabled=false</span><br><span class="line"># 连接器在关闭连接之前等待另一个HTTP请求的时间。 未设置时，将使用连接器的特定于容器的默认值。 使用值-1表示没有（即无限）超时</span><br><span class="line">server.connection-timeout=2000</span><br><span class="line"># 错误控制器的路径</span><br><span class="line">server.error.path=/error</span><br><span class="line"># 是否启用HTTP / 2支持，如果当前环境支持它。</span><br><span class="line">server.http2.enabled=false</span><br><span class="line"># 服务器端口默认为:8080</span><br><span class="line">server.port=8084</span><br><span class="line"># SP servlet的类名。</span><br><span class="line">server.servlet.jsp.class-name=org.apache.jasper.servlet.JspServlet</span><br><span class="line"># 主调度程序servlet的路径。</span><br><span class="line">server.servlet.path=/home</span><br><span class="line"># 会话cookie名称</span><br><span class="line">server.servlet.session.cookie.name=propertydemo</span><br><span class="line"></span><br><span class="line">#------------------------------</span><br><span class="line"># HTTP encoding</span><br><span class="line">#------------------------------</span><br><span class="line"># HTTP请求和响应的字符集。 如果未明确设置，则添加到“Content-Type”标头。</span><br><span class="line">spring.http.encoding.charset=UTF-8</span><br><span class="line"># 是否启用http编码支持。</span><br><span class="line">spring.http.encoding.enabled=true</span><br><span class="line">#--------------------</span><br><span class="line"># MULTIPART (MultipartProperties)</span><br><span class="line">#--------------------</span><br><span class="line"># 是否启用分段上传支持</span><br><span class="line">spring.servlet.multipart.enabled=true</span><br><span class="line"># 上传文件的中间位置</span><br><span class="line">spring.servlet.multipart.location=/log</span><br><span class="line"># 最大文件的大小</span><br><span class="line">spring.servlet.multipart.max-file-size=1MB</span><br><span class="line"># 最大请求大小</span><br><span class="line">spring.servlet.multipart.max-request-size=10MB</span><br><span class="line"># 是否在文件或参数访问时懒惰地解析多部分请求。</span><br><span class="line">spring.servlet.multipart.resolve-lazily=false</span><br><span class="line">#--------------------------------------------</span><br><span class="line"># SPRING SESSION JDBC (JdbcSessionProperties)</span><br><span class="line">#--------------------------------------------</span><br><span class="line"># cron表达式用于过期的会话清理作业</span><br><span class="line">spring.session.jdbc.cleanup-cron=0 * * * * *</span><br><span class="line"># 数据库模式初始化模式</span><br><span class="line">spring.session.jdbc.initialize-schema=embedded</span><br><span class="line"># 用于初始化数据库模式的SQL文件的路径</span><br><span class="line">spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql</span><br><span class="line"># 用于存储会话的数据库表的名称</span><br><span class="line">spring.session.jdbc.table-name=SPRING_SESSION</span><br><span class="line"></span><br><span class="line">#----------------------------------</span><br><span class="line"># MONGODB 数据库配置</span><br><span class="line">#----------------------------------</span><br><span class="line"># 数据库名称</span><br><span class="line">spring.data.mongodb.database=demo</span><br><span class="line"># host 配置</span><br><span class="line">spring.data.mongodb.host=127.0.0.1</span><br><span class="line"># 登录用户名</span><br><span class="line">spring.data.mongodb.username=property</span><br><span class="line"># 登录密码</span><br><span class="line">spring.data.mongodb.password=123456</span><br><span class="line"># 端口号，自己根据安装的mongodb端口配置</span><br><span class="line">spring.data.mongodb.port=9008</span><br><span class="line"># 要启用的Mongo存储库的类型</span><br><span class="line">spring.data.mongodb.repositories.type=auto</span><br><span class="line"># 连接数据uri</span><br><span class="line">spring.data.mongodb.uri=mongodb://localhost/test</span><br><span class="line"></span><br><span class="line">#---------------------------------------</span><br><span class="line"># DATASOURCE 数据库配置</span><br><span class="line">#---------------------------------------</span><br><span class="line"># MySql jdbc Driver</span><br><span class="line">spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver</span><br><span class="line"># 连接数据库</span><br><span class="line"># demo表示的是你创建的数据库;</span><br><span class="line">spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useSSL=false&amp;requireSSL=false&amp;characterEncoding=UTF-8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC</span><br><span class="line"># 数据库用户名</span><br><span class="line">spring.datasource.username=root</span><br><span class="line"># 数据库密码</span><br><span class="line">spring.datasource.password=123456</span><br><span class="line">#-----------------------------------</span><br><span class="line"># Jpa使用</span><br><span class="line">#-----------------------------------</span><br><span class="line"># 目标数据库进行操作，默认情况下自动检测。可以使用“databasePlatform”属性设置。</span><br><span class="line">#spring.jpa.database= demo1</span><br><span class="line"># 要操作的目标数据库的名称，默认情况下自动检测。 也可以使用“Database”枚举来设置。</span><br><span class="line">#spring.jpa.database-platform=DEMO</span><br><span class="line"># DDL模式 一般有这几种方式,Spring Boot会根据是否认为您的数据库是嵌入式的，为您选择一个默认值</span><br><span class="line"># update: 更新架构时，使用;</span><br><span class="line">spring.jpa.hibernate.ddl-auto=update</span><br><span class="line"># 是否启用SQL语句的日志记录</span><br><span class="line">spring.jpa.show-sql=true</span><br><span class="line"></span><br><span class="line">#----------------------------------------</span><br><span class="line"># TESTING PROPERTIES</span><br><span class="line">#----------------------------------------</span><br><span class="line"># 要替换的现有DataSource的类型</span><br><span class="line">spring.test.database.replace=any</span><br><span class="line"># MVC打印选项</span><br><span class="line">spring.test.mockmvc.print=default</span><br><span class="line"></span><br><span class="line"># ---------------大家查看文档进行配置，不一一列举了----------------------</span><br><span class="line"></span><br><span class="line">#  各个属性注解在查看常用配置文件application.properties中</span><br><span class="line"></span><br><span class="line"># FREEMARKER</span><br><span class="line"></span><br><span class="line"># DEVTOOLS配置</span><br><span class="line"></span><br><span class="line"># SPRING HATEOAS</span><br><span class="line"></span><br><span class="line"># HTTP message conversion</span><br><span class="line"></span><br><span class="line"># GSON</span><br><span class="line"></span><br><span class="line"># JDBC</span><br><span class="line"></span><br><span class="line"># JEST (Elasticsearch HTTP client) (JestProperties)</span><br><span class="line"></span><br><span class="line"># CASSANDRA (CassandraProperties)</span><br><span class="line"># --------------------------等等----------------------------------</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>查看常用配置文件<a href="https://github.com/eirunye/SpringBoot_Property/blob/master/src/main/resources/application.properties" target="_blank" rel="noopener">application.properties</a></p>
</blockquote>
<h3 id="自定义属性"><a href="#自定义属性" class="headerlink" title="自定义属性"></a>自定义属性</h3><p> 由于有时为了方便项目的开发维护，我们可能需要到自定义配置属性，接下来我们也来搞一下自定义属性配置。</p>
<blockquote>
<p>在<strong>application.properties</strong>自定义配置属性:</p>
</blockquote>
<p>1.application.properties添加:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#--------------------------------</span><br><span class="line"># 自定义属性</span><br><span class="line">#--------------------------------</span><br><span class="line">com.eirunye.defproname=&quot;root&quot;</span><br><span class="line">com.eirunye.defpropass=&quot;123456&quot;</span><br></pre></td></tr></table></figure>
<p>2.在<strong>DefPropertyController.class</strong>引用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">@RestController</span><br><span class="line">public class DefPropertyController &#123;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;com.eirunye.defproname&#125;&quot;)</span><br><span class="line">    private String defProName;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;com.eirunye.defpropass&#125;&quot;)</span><br><span class="line">    private String defProPass;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value = &quot;/defproprety&quot;)</span><br><span class="line">    public String defPropretyUser() &#123;</span><br><span class="line">        return &quot;这个自定义属性名为: &quot; + defProName + &quot;, 密码为:&quot; + defProPass;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>注意</strong><br>在获取自定义属性时一定要严格按照配置文件来获取并且Value里面的字符串一定是<code>$</code>+花括号<code>{***}</code>,花括号里面的<code>***</code>表示为:<code>application.properties里面自定义的字符串</code>，所以本例就是表示为:<code>@Value(&quot;${com.eirunye.defproname}&quot;)</code> 。<br>如果<code>application.properties</code>有自定义为<code>test.ok=&quot;haha&quot;</code>,同样的获取方式为:<code>@Value(&quot;${test.ok}&quot;)</code></li>
</ul>
<p>3.测试<code>这里暂未使用测试代码的方式，后面的文章会讲到</code></p>
<ul>
<li>IntelliJ IDEA访问<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:8084/defproprety</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3012005-d5479d7990d69320.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="def_idea.png"></p>
<ul>
<li><a href="https://www.getpostman.com/" target="_blank" rel="noopener">Postman访问</a></li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/3012005-a52caf3352427bd0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="def_postman.png"></p>
<blockquote>
<p>通过Bean的形式获取</p>
</blockquote>
<p>假如我们遇到这样情况，自定义属性多，然后每个都是通过<code>@Value(${&quot;&quot;})</code>方式的话可能会很容易出错，那么我们可以采用以下方式。</p>
<p>1.新建一个<code>Properties.class</code><br>添加<code>@ConfigurationProperties(prefix = &quot;com.eirunye&quot;)</code>//表示的是通过自定义属性查找，如果自定义是:<code>test.ok=haha</code>,则该这样表示:<code>@ConfigurationProperties(prefix = &quot;test&quot;)</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">@ConfigurationProperties(prefix = &quot;com.eirunye&quot;)//添加该注解</span><br><span class="line">public class Properties &#123;</span><br><span class="line">    private String defproname;</span><br><span class="line">    private String defpropass;</span><br><span class="line">//  get/set方法</span><br><span class="line">    public String getDefproname() &#123;</span><br><span class="line">        return defproname;</span><br><span class="line">    &#125;</span><br><span class="line">    public void setDefproname(String defproname) &#123;</span><br><span class="line">        this.defproname = defproname;</span><br><span class="line">    &#125;</span><br><span class="line">    public String getDefpropass() &#123;</span><br><span class="line">        return defpropass;</span><br><span class="line">    &#125;</span><br><span class="line">    public void setDefpropass(String defpropass) &#123;</span><br><span class="line">        this.defpropass = defpropass;</span><br><span class="line">    &#125;&#125;</span><br></pre></td></tr></table></figure>
<p>2.在controller包下创建 <code>DefBeanPropertyController.class</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@RestController</span><br><span class="line">public class DefBeanPropertyController &#123;</span><br><span class="line">   //通过 Autowired注解来获取到 Properties属性，注:Autowired是按类型进行装配，可获取它所装配类的属性</span><br><span class="line">    @Autowired</span><br><span class="line">    Properties properties;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value = &quot;/bean/defproperty&quot;)</span><br><span class="line">    public String getDefBeanProperties() &#123;</span><br><span class="line">        return &quot;这是通过Bean注解的方式获取属性: &quot; + properties.getDefproname() + &quot;,密码为: &quot; + properties.getDefpropass();</span><br><span class="line">    &#125;&#125;</span><br></pre></td></tr></table></figure>
<p>3.在项目的入口文件<strong>Application</strong>添加注解<strong>@EnableConfigurationProperties</strong><br>最后加上包名不然可能找不到扫描文件如:<code>@EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class})</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@SpringBootApplication</span><br><span class="line">@EnableConfigurationProperties(&#123;com.eirunye.defpropertys.bean.Properties.class&#125;)//添加注解bean的扫描文件</span><br><span class="line">public class DefpropertysApplication &#123;</span><br><span class="line"></span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		SpringApplication.run(DefpropertysApplication.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>4.测试<br>IntelliJ IDEA访问</p>
<p><img src="https://upload-images.jianshu.io/upload_images/3012005-e83c94a826f2309d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="bean_def_idea.png"></p>
<blockquote>
<p>创建文件xxx.properties文件方式</p>
</blockquote>
<p>我们可以自己创建一个自定义属性的文件如本例<strong>def.properties</strong>，(<em>注:一般都是以 <strong>.properties</strong> 文件结尾</em>)</p>
<p>1.添加自定义<code>def.properties</code>配置如下:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/3012005-7730447bb95f7fee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/500" alt="def_file.png"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">#--------------------------------</span><br><span class="line"># 自定义属性</span><br><span class="line">#--------------------------------</span><br><span class="line"># 用户名</span><br><span class="line">com.eirunye.defineuser=&quot;property&quot;</span><br><span class="line"># 年龄</span><br><span class="line">com.eirunye.defineage=20</span><br></pre></td></tr></table></figure>
<p>2.创建 <code>DefineProperties.class</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">@Configuration</span><br><span class="line">@ConfigurationProperties(prefix = &quot;com.eirunye&quot;)//添加注解 ConfigurationProperties &quot;com.eirunye&quot;表示的是自定义属性</span><br><span class="line">@PropertySource(&quot;classpath:defines.properties&quot;)// 添加注解 PropertySource 该注解能根据路径扫描到我们的文件</span><br><span class="line">public class DefineProperties &#123;</span><br><span class="line">//    这里可以通过@Value(&quot;$&#123;&#125;&quot;)方式添加,我已经屏蔽掉了，直接通过ConfigurationProperties注解的方式</span><br><span class="line">//    @Value(&quot;$&#123;com.eirunye.defineuser&#125;&quot;)</span><br><span class="line">    private String defineuser;</span><br><span class="line">//    @Value(&quot;$&#123;com.eirunye.defineage&#125;&quot;)</span><br><span class="line">    private int defineage;</span><br><span class="line">// get/set方法</span><br><span class="line">    public String getDefineuser() &#123;</span><br><span class="line">        return defineuser;</span><br><span class="line">    &#125;</span><br><span class="line">    public void setDefineuser(String defineuser) &#123;</span><br><span class="line">        this.defineuser = defineuser;</span><br><span class="line">    &#125;</span><br><span class="line">    public int getDefineage() &#123;</span><br><span class="line">        return defineage;</span><br><span class="line">    &#125;</span><br><span class="line">    public void setDefineage(int defineage) &#123;</span><br><span class="line">        this.defineage = defineage;</span><br><span class="line">    &#125;&#125;</span><br></pre></td></tr></table></figure>
<p>3.在<code>DefinePropertiesController.class</code>引用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@RestController</span><br><span class="line">public class DefinePropertiesController &#123;</span><br><span class="line">    @Autowired</span><br><span class="line">    DefineProperties defineProperties;</span><br><span class="line">    @RequestMapping(value = &quot;define/Properties&quot;)</span><br><span class="line">    public String getDefinePropertiesData()&#123;</span><br><span class="line">        return &quot;新建文件自定义属性姓名：&quot;+defineProperties.getDefineuser()+&quot;,新建文件自定义属性年龄：&quot;+defineProperties.getDefineage();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>4.别忘了在Application里面添加配置<code>@EnableConfigurationProperties</code>,即：<code>@EnableConfigurationProperties({com.eirunye.defpropertys.bean.Properties.class,com.eirunye.defpropertys.bean.DefineProperties.class})</code></p>
<p>5.测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:8084/define/Properties</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/3012005-9fb441bce0fc8ecf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="def_idea_show.png"></p>
<blockquote>
<p><strong>下载application.properties<a href="https://github.com/eirunye/SpringBoot_defpropertys" target="_blank" rel="noopener">案例demo</a></strong></p>
</blockquote>
<h2 id="application-yml配置"><a href="#application-yml配置" class="headerlink" title="application.yml配置"></a>application.yml配置</h2><p>由于application.properties配置有点繁琐，简洁是我们非常喜欢的，那么在Spring Boot程序里面当然也是可以用.yml文件来配置的，接下来让我们进入对.yml文件的一些相关配置吧，官方文档<a href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml" target="_blank" rel="noopener">Using YAML Instead of Properties</a>。</p>
<h3 id="常见的配置"><a href="#常见的配置" class="headerlink" title="常见的配置"></a>常见的配置</h3><p>首先在src目录下的/main/resource文件夹中新建<code>application.yml、application-dev.yml、application-prod.yml</code>三个文件，删除<code>application.properties</code>文件，目录结构如下:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">|--src</span><br><span class="line">   |--main</span><br><span class="line">      |--resources</span><br><span class="line">         |--application.yml</span><br><span class="line">         |--application-dev.yml</span><br><span class="line">         |--application-prod.yml</span><br></pre></td></tr></table></figure></p>
<p>相信很多人要骂街了，这什么情况不是说,yml配置很给力吗？怎么还有创建那么多文件，这不是比上面的  <code>application.properties</code>配置还多此一举吗？莫急接下来让我们来看看.yml的配置之后，我相信你肯定在项目中会喜欢用它。</p>
<p>常见的配置属性如下: 【注意: 这里属性之间的间隔必须按照要求而来,如:冒号后面要空格】<br><code>application.yml</code> 配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  profiles:</span><br><span class="line">    active: dev   #引用 application-dev.yml文件,这里我们可以改为 prod,表示引用application-prod.yml文件</span><br><span class="line">  datasource:</span><br><span class="line">      driver-class-name: com.mysql.cj.jdbc.Driver</span><br><span class="line">      url: jdbc:mysql://127.0.0.1:3306/demo?useSSL=false&amp;requireSSL=false&amp;characterEncoding=UTF-8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC</span><br><span class="line">      username: root</span><br><span class="line">      password: 12346</span><br><span class="line">  jpa:</span><br><span class="line">    hibernate:</span><br><span class="line">      ddl-auto: update</span><br><span class="line">    show-sql: true</span><br><span class="line">  data:</span><br><span class="line">    mongodb:</span><br><span class="line">      host: 127.0.0.1</span><br><span class="line">      uri: mongodb://localhost/test</span><br><span class="line">      username: root</span><br><span class="line">      password: 123456</span><br><span class="line">      database: test</span><br><span class="line">  test:</span><br><span class="line">    database:</span><br><span class="line">      replace: any</span><br><span class="line">    mockmvc:</span><br><span class="line">      print: default</span><br><span class="line">  servlet:</span><br><span class="line">    multipart:</span><br><span class="line">      enabled: true</span><br></pre></td></tr></table></figure>
<p><code>application-dev.yml</code> 可以当成正式服务器端口</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">server:</span><br><span class="line">  port: 8084</span><br></pre></td></tr></table></figure>
<p><code>application-prod.yml</code> 可以当成测试服务器端口</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">server:</span><br><span class="line">  port: 8080</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>查看更多<a href="https://github.com/eirunye/SpringBoot_Property/blob/master/src/main/resources/application.yml" target="_blank" rel="noopener">.yml配置</a></strong></li>
</ul>
<h3 id="自定义yml配置"><a href="#自定义yml配置" class="headerlink" title="自定义yml配置"></a>自定义yml配置</h3><blockquote>
<p>在application.yml配置</p>
</blockquote>
<p>1.和上面的application.properties类似，但是需要注意的是<em>格式问题</em><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">com:</span><br><span class="line">  eirunye:</span><br><span class="line">    ymlname: ymlroot</span><br><span class="line">    ymlpass: yml123456</span><br></pre></td></tr></table></figure></p>
<p>2.通过<code>@Value(&quot;${com.eirunye.ymlname}&quot;)</code>获取</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@RestController</span><br><span class="line">public class YmlPropertiesController&#123;</span><br><span class="line">    @Value(&quot;$&#123;com.eirunye.ymlname&#125;&quot;)</span><br><span class="line">    private String ymlname;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;com.eirunye.ymlpass&#125;&quot;)</span><br><span class="line">    private String ymlpass;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value = &quot;yml/proprety&quot;)</span><br><span class="line">    public String getYmlPropreties() &#123;</span><br><span class="line">        return &quot;这个自定义属性名为: &quot; + ymlname+ &quot;, 密码为:&quot; + ymlpass;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>3.测试—访问:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:8084/yml/proprety</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/3012005-eae9ed7abb04ee43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="yml_def.png"></p>
<blockquote>
<p>通过Bean方式获取和application.properties方式一样</p>
</blockquote>
<p>1.创建YmlPropertyBean.class</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@ConfigurationProperties(prefix = &quot;com.eirunye&quot;)//添加该注解</span><br><span class="line">public class YmlPropertyBean &#123;</span><br><span class="line"></span><br><span class="line">    private String ymlname;</span><br><span class="line">    private String ymlpass;</span><br><span class="line">// get/set方法.....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>2.在 <code>YmlPropertyBeanController.class</code>引用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@RestController</span><br><span class="line">public class YmlPropertyBeanController &#123;</span><br><span class="line">    @Autowired</span><br><span class="line">    YmlPropertyBean propertyBean;</span><br><span class="line">    @RequestMapping(value = &quot;/bean/ymlproperty&quot;)</span><br><span class="line">    public String getYmlPropertyData()&#123;</span><br><span class="line">        return &quot;这个bean自定义属性名为: &quot; + propertyBean.getYmlname()+ &quot;, 密码为:&quot; + propertyBean.getYmlpass();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>3.测试-访问</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http://localhost:8084/bean/ymlproperty</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/3012005-5551aabdebf638ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="yml_bean_def.png"></p>
<blockquote>
<p><strong>下载.yml<a href="https://github.com/eirunye/SpringBoot_defineyml" target="_blank" rel="noopener">案例demo</a></strong></p>
</blockquote>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>   1.本篇主要讲得配置文件，到此就结束了，在开发中这是我们经常用到。<br>   2.在本篇有些相关配置，本例的代码实例还没涉及到，接下来会继续结合相关的配置案例继续更新。<br>   3.相信大家也有所掌握。</p>

      
    </div>

    

    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>继续努力哦！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/Reward_Weichat.png" alt="Eirunye 微信支付"/>
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Eirunye</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://eirunye.gitee.io/2018/08/20/Spring-Boot-配置文件设置-三/" title="Spring Boot 配置文件设置(三)">https://eirunye.gitee.io/2018/08/20/Spring-Boot-配置文件设置-三/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
          
            <a href="/tags/Spring-Boot/" rel="tag"><i class="fa fa-tag"></i> Spring Boot</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/08/14/Spring-Boot-项目创建-二/" rel="next" title="Spring Boot 项目创建(二)">
                <i class="fa fa-chevron-left"></i> Spring Boot 项目创建(二)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/08/22/Spring-Boot-项目如何搭建-四/" rel="prev" title="Spring Boot 项目如何搭建(四)">
                Spring Boot 项目如何搭建(四) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODk2MC8xNTQ4OA=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/eirunye_icon.png"
                alt="Eirunye" />
            
              <p class="site-author-name" itemprop="name">Eirunye</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">11</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/eirunye" target="_blank" title="GitHub" rel="external nofollow"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:yrungen@gmail.com" target="_blank" title="E-Mail" rel="external nofollow"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.jianshu.com/u/447fdef5fb8f" target="_blank" title="简书" rel="external nofollow"><i class="fa fa-fw fa-heartbeat"></i>简书</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://segmentfault.com/blog/iconye_vue" target="_blank" title="Segmentfault" rel="external nofollow"><i class="fa fa-fw fa-link"></i>Segmentfault</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.zhihu.com/people/yi-ke-id/activities" target="_blank" title="知乎" rel="external nofollow"><i class="fa fa-fw fa-ioxhost"></i>知乎</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://stackoverflow.com/" target="_blank" title="StackOverflow" rel="external nofollow"><i class="fa fa-fw fa-stack-overflow"></i>StackOverflow</a>
                  
                </span>
              
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank" rel="external nofollow">
                <img src="/images/cc-by-nc-sa.svg" alt="Creative Commons" />
              </a>
            </div>
          

          
          

          
            
          
          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Eirunye</span>

  

  
</div>


  



<div class="powered-by">
  <i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
    本站访客数:<span id="busuanzi_value_site_pv"></span>
  </span>
  </div>
  <div class="powered-by">由 <a class="theme-link" target="_blank" rel="external nofollow" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a class="theme-link" target="_blank" rel="external nofollow" href="https://theme-next.org">NexT.Pisces</a> v6.4.1</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
















  
  







  





  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  

  
  
    <script id="ribbon" type="text/javascript" size="300" alpha="0.6"  zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.4.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.4.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.4.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.4.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.4.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.4.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.4.1"></script>



  



  
    <script type="text/javascript">
      window.livereOptions = {
        refer: '2018/08/20/Spring-Boot-配置文件设置-三/'
      };
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  










  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      
        // ref: https://github.com/ForbesLindesay/unescape-html
        var unescapeHtml = function(html) {
          return String(html)
            .replace(/&quot;/g, '"')
            .replace(/&#39;/g, '\'')
            .replace(/&#x3A;/g, ':')
            // replace all the other &#x; chars
            .replace(/&#(\d+);/g, function (m, p) { return String.fromCharCode(p); })
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>')
            .replace(/&amp;/g, '&');
        };
      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                content = unescapeHtml(content);
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  

  
  

  

  

  

  
  
  
  <script src="/lib/bookmark/bookmark.min.js?v=1.0"></script>
  <script type="text/javascript">
  
    bookmark.scrollToMark('auto', "#更多");
  
  </script>


  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function (e) {
        var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
          return $(e).text()
        }).toArray().join('\n')
        var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
        
          if(result)$(this).text('复制成功')
          else $(this).text('复制失败')
        
        $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('复制')
        }, 300)
      }).append(e)
    })
  </script>


</body>
</html>
